package com.wn.service;

import com.alipay.api.FileItem;
import com.wn.common.pojo.*;
import com.wn.mapper.CityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: hurricane_travel
 * @BelongsPackage: com.wn.service
 * @Author: lhq
 * @Description:
 */
@Service
@Transactional
public class CityService {

    @Autowired
    private CityMapper cityMapper;

    /**
     * 获得所有城市的信息
     * @return
     */
    public Rsp getAllCity() {

        return new Rsp().success(cityMapper.getAllCity());
    }

    /**
     * 根据省名搜索城市名
     * @param cityName
     */
    public Rsp getCityByName(String cityName) {

        List<City> city = cityMapper.getCityByName(cityName);
        return Rsp.success(city);

    }





    /*级联搜索城市（私人定制菜单）*/
    public Rsp getCityList(ReqCity reqCity) {
        int levle = 0;
        List<ResCity> resCityList = new ArrayList<>();

        if("province".equals(reqCity.getType())){
            //如果是搜索省份的请求
            levle=1;
            resCityList =cityMapper.getAllProvince(levle);
        }else if("city".equals(reqCity.getType())){
            //如果是搜索城市的请求
            levle=2;
            int parentCode = reqCity.getProvince_code();
            resCityList = cityMapper.getAllCityByParentCode(levle,parentCode);
        }else if("area".equals(reqCity.getType())){
            //如果是搜索城市的请求
            levle=3;
            int parentCode = reqCity.getCity_code();
            resCityList = cityMapper.getAllCityByParentCode(levle,parentCode);
        }

        return new Rsp().success(resCityList);
    }

    /**
     * 目的地的级联查询
     * @param
     * @return
     */
    public Rsp getDestinationByName(ReqDestination deqtination) {
        int levle = 0;
        List<ResCity> resDestinationList = new ArrayList<>();

//        if("province".equals(deqtination.getDestinationType())){
//            //如果是搜索国家的请求
//            levle=1;
//            resDestinationList =cityMapper.getAllProvince(levle);
//        }else if("city".equals(deqtination.getDestinationType())){
//            //如果是搜索省份的请求
//            levle=2;
//            int parentCode = deqtination.getDestinationInContory_code();
//            resDestinationList = cityMapper.getAllCityByParentCode(levle,parentCode);
//        }else if("area".equals(deqtination.getDestinationType())){
//            //如果是搜索城市的请求
//            levle=3;
//            int parentCode = deqtination.getDestinationInCity_code();
//            resDestinationList = cityMapper.getAllCityByParentCode(levle,parentCode);
//        }
        resDestinationList =cityMapper.getDestinationByParentId(deqtination.getParentId());
        return new Rsp().success(resDestinationList);



    }
    /*根据城市id拿城市对象*/
    public Rsp getCityById(Integer cityId) {
        City city = cityMapper.selectByPrimaryKey(cityId);
        if(StringUtils.isEmpty(city)){
            return new Rsp().fail();
        }
        return new Rsp().success(city);
    }
    /*根据城市识别码拿到城市对象*/
    public Rsp getCityByRegionCode(Integer cityRegionCode) {
        City city = cityMapper.selectByRegionCode(cityRegionCode);
        if(StringUtils.isEmpty(city)){
            return new Rsp().fail();
        }
        return new Rsp().success(city);
    }
}
